package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;


import java.sql.SQLException;

/**
 * @author Kuke
 * @date 2022/3/7 17:05
 * 针对前台用户的数据访问接口实现
 */
public class UserDaoImpl implements UserDao {


    /**
     * 通过用户名查询用户
     * @param username  输入的用户名
     * @return 返回的用户实体
     */
    @Override
    public User selectUserByUsername(String username) throws SQLException {

        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user where username = ?" ;
        //查询
        User user = qr.query(sql, new BeanHandler<User>(User.class), username);
        return user;
    }

    /**
     * 添加用户
     * @param user 用户实体
     */
    @Override
    public void addUser(User user) throws SQLException {

        //创建执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;

        //sql

        String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)" ;

        //更新
        int count = qr.update(sql,
                user.getUid(),
                user.getUsername(),
                user.getPassword(),
                user.getName(),
                user.getEmail(),
                user.getTelephone(),
                user.getBirthday(),
                user.getSex(),
                user.getState(),
                user.getCode()
        );
        System.out.println(count);

    }

    /**
     * 根据用户激活码查询指定的用户
     * @param code 激活码
     * @return 返回用户实体
     */
    @Override
    public User selectUserByCode(String code) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user where code =?" ;
        //查询
        User user = qr.query(sql,new BeanHandler<User>(User.class),code) ;
        return user;
    }

    /**
     * 更新用户
     * @param user 用户实体
     */
    @Override
    public void updateUser(User user) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "update user set  username = ?,password=?,name =?,email=?,telephone=?,birthday=?,sex=?,state=?,code=? where uid  =?";
        int count = qr.update(sql,
                user.getUsername(),
                user.getPassword(),
                user.getName(),
                user.getEmail(),
                user.getTelephone(),
                user.getBirthday(),
                user.getSex(),
                user.getState(),
                user.getCode(),
                user.getUid());
        System.out.println(count) ;
    }


    /**
     * 数据访问接口查询用户
     * @param username  用户名
     * @param password  密码
     * @return  返回用户实体
     */
    @Override
    public User selectUserByUsernameAndPwd(String username, String password) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user where username = ? and password = ?" ;
        //执行
        User user = qr.query(sql, new BeanHandler<User>(User.class), username, password);
        return user;
    }
}
